package com.jeasonzhao.report.engine.repo;

import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.report.engine.Configuration;
import com.jeasonzhao.report.model.Report;
import com.jeasonzhao.report.model.ReportDatabaseConfiguration;
import com.jeasonzhao.report.msg.MessageCodes;

public class DatabaseConfigurationRepository
{
    private java.util.Hashtable<String,ReportDatabaseConfiguration> m_hashDatabaseCfg = new java.util.Hashtable<String,ReportDatabaseConfiguration>();
    private static volatile DatabaseConfigurationRepository m_instance = null;
    public static DatabaseConfigurationRepository getInstance()
    {
        if(null == m_instance)
        {
            synchronized(DatabaseConfigurationRepository.class)
            {
                m_instance = new DatabaseConfigurationRepository();
            }
        }
        return m_instance;
    }

    private DatabaseConfigurationRepository()
    {
    }

    private String alignId(String strDatabaseId)
    {
        return Algorithms.isEmpty(strDatabaseId) ? null : strDatabaseId.trim().toLowerCase();
    }

    public void add(ReportDatabaseConfiguration ds)
    {
        if(null != ds)
        {
            Configuration.getInstance().getLogger().info(MessageCodes.format(MessageCodes.CFG_ADDDATABASE,MessageCodes.getLongDescription(ds)));
            m_hashDatabaseCfg.put(alignId(ds.getDatabaseId()),ds);
        }
    }

    public ReportDatabaseConfiguration get(Report report)
    {
        return null == report ? null : get(report.getDatabaseId());
    }

    public ReportDatabaseConfiguration get(String strDatabaseId)
    {
        return m_hashDatabaseCfg.get(alignId(strDatabaseId));
    }

    public void remove(String strId)
    {
        if(null != strId)
        {
            m_hashDatabaseCfg.remove(strId);
        }
    }
}
